package com.wmapp.web;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wmapp.domain.CDR;
import com.wmapp.domain.History;
import com.wmapp.domain.MCUStatistic;
import com.wmapp.domain.MCUVersion;
import com.wmapp.domain.PageResultWrapper;
import com.wmapp.domain.Participant;
import com.wmapp.language.Lang;
import com.wmapp.service.ManagerService;
import com.wmapp.service.ViewService;
import com.wmapp.utils.HttpUtils;
import net.sf.json.JSONArray;

@Controller
@RequestMapping("/view")
public class ViewController {
	private static Log log = LogFactory.getLog(ViewController.class);
	
	private ViewService viewService;
	private ManagerService managerService;
	
	private Lang lang;
	
	@Autowired
	public void setLang(Lang lang) {
		this.lang = lang;
	}
	
	@Autowired
	public void setViewService(ViewService viewService) {
		this.viewService = viewService;
	}
	
	@Autowired
	public void setManagerService(ManagerService managerService) {
		this.managerService = managerService;
	}

	// ----------------------- MCU version ----------------------
	@RequestMapping(value="/mcu/version",method=RequestMethod.GET)
	public String showMCUVersion(Model model){
		MCUVersion mcuVersion=viewService.getMCUVersion();
		model.addAttribute("model", mcuVersion);
		return "mcuVersion";
	}
	
	// ----------------------- MCU statistic ----------------------
	@RequestMapping(value="/mcu/statistic",method=RequestMethod.GET)
	public String showMCUStatistic(Model model){
		MCUStatistic mcuStatistic=viewService.getMCUStatistic();
		model.addAttribute("model", mcuStatistic);
		return "mcuStatistic";
	}
	
	@RequestMapping(value="/participants",method=RequestMethod.GET)
	@ResponseBody
	public String viewParticipants(){
		List<Participant> list=managerService.queryParticipantList();
		List<Participant> last=new ArrayList<Participant>();
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Participant participant = (Participant) iterator.next();
			if(participant.getData()!=null&&!participant.getData().equals("")){
				last.add(participant);
			}
		}
		
		JSONObject jObj=HttpUtils.domainList2GridRecords(last, Participant.class, Participant.GRID_COLS_FOR_VIEW, "number");
		return jObj.toString();
	}
	
	@RequestMapping(value="/cdr",method=RequestMethod.GET)
	@ResponseBody
	public String queryCDR(@RequestParam(value="start",required=true) int start,
			@RequestParam(value="pagelimit",required=true) int pagelimit,
            @RequestParam(value="browse",required=true) boolean browse,
            @RequestParam(value="start_date",required=true) String start_date,
            @RequestParam(value="end_date",required=true) String end_date,
            @RequestParam(value="caller",required=true) String caller,
            @RequestParam(value="called",required=true) String called){
		try {
			JSONObject ret=new JSONObject();
			PageResultWrapper wrapper=viewService.queryCDR(start, pagelimit, browse, start_date, end_date, caller, called);
			ret.put("total", wrapper.getTotal());
            ret.put("count", wrapper.getCount());
            ret.put("billtime", wrapper.getBilltime());
			ret.put("result", HttpUtils.domainList2GridRecords(wrapper.getResult(), CDR.class, CDR.GRID_COL_FIELDS, "id"));
			
			return ret.toString();
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}

    @RequestMapping(value="/history",method=RequestMethod.GET)
	@ResponseBody
	public String queryHistory(@RequestParam(value="start_date",required=true) String start_date,
			@RequestParam(value="end_date",required=true) String end_date){
        String ret="";
		try {			
			List<History> list=viewService.queryHistory(start_date, end_date);
			JSONArray jObj = HttpUtils.domainList2ChartJson(list, History.class, History.GRID_COLS);
			ret =  jObj.toString();
            return ret;
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	//-----------------MCU datetime--------------
	@RequestMapping(value="/mcuDatetime",method=RequestMethod.GET)
	@ResponseBody
	public String getMCUDatetime(){
		String datetime=viewService.getMCUDatetime();
		//String datetime="2011-01-01 12:57";
		JSONObject jObject=new JSONObject();
		jObject.put("datetime", datetime);
		//log.debug(jObject.toString());
		return jObject.toString();
	}
}
